<?php

namespace App\Http\Controllers\Admin\V1;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\RoleRequest;
use App\Models\Examination;
use App\Models\Permission;
use App\Models\Role;
use App\Transformers\Admin\RoleTransformer;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;

class RoleController extends Controller
{
    /**
     * 角色列表
     *
     * @return Response
     */
    public function index()
    {
        // 查询数据
        $data = Role::pimp()->paginate($this->pageLimit());

        // 返回数据
        return $this->response->paginator($data, new RoleTransformer());
    }

    /**
     * 添加角色
     *
     * @param RoleRequest $request
     * @param Role $role
     * @return Response
     */
    public function store(RoleRequest $request, Role $role)
    {
        //获取输入的权限
        $allPermission = Permission::query()
            ->whereIn('id', $request->input('permission_id'))
            ->pluck('id');
        try {
            DB::beginTransaction();
            //创建角色
            $role->name = $request->input('name');
            $role->cn_name = $request->input('cn_name');
            $role->save();
            //分配权限
            if (!empty($allPermission)) {
                $role->syncPermissions($allPermission);
            }
            //插入数据
            DB::commit();
            return $this->response->created();
        } catch (Exception $e) {
            DB::rollBack();
            report($e);
            return $this->response->errorBadRequest('参数错误');
        }
    }

    /**
     * 角色详情
     *
     * @param Role $role
     * @return Response
     */
    public function show(Role $role)
    {
        return $this->response->item($role, new RoleTransformer());
    }

    /**
     * 修改角色
     *
     * @param RoleRequest $request
     * @param Role $role
     * @return Response
     */
    public function update(RoleRequest $request, Role $role)
    {
        if ($role->name == 'super_admin'){
            return $this->response->errorBadRequest('不能操作超级管理员');
        }
        //获取输入权限
        $allPermission = Permission::query()
            ->whereIn('id', $request->input('permission_id'))
            ->pluck('id');
        try {
            DB::beginTransaction();
            //更改角色
            $role->cn_name = $request->input('cn_name');
            $role->save();
            //分配权限
            $role->syncPermissions($allPermission);
            DB::commit();
            return $this->response->noContent();
        } catch (Exception $e) {
            DB::rollBack();
            report($e);
            return $this->response->errorBadRequest('参数错误');
        }
    }

    /**
     * 删除角色
     *
     * @param $ids
     * @return Response
     */
    public function destroy($ids)
    {
        // 分割传过来的id
        $idArr = explode(',', $ids);

        foreach (Role::whereIn('id', $idArr)->get() as $role) {

            if ($role->name == 'super_admin'){
                return $this->response->errorBadRequest('不能删除超级管理员');
            }

            //执行删除
            $role->delete();
        }

        return $this->response->noContent();
    }
}
